package com.wanpinghui.supplier.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.wphlibrary.model.base.AppUpdateManager;
import com.yunshl.yunshllibrary.manager.UpdateManager;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.io.File;
import java.io.FileInputStream;
import java.util.Locale;

/**
 * Created by xiongning on 17-12-7.
 */

public class FileUtil {

    private static BaseActivity activity;

    public static void setActivity(BaseActivity activity) {
        FileUtil.activity = activity;
    }

    public static BaseActivity getActivity() {
        return activity;
    }

    public static String getFileExtType(String filePath) {
        File file = new File(filePath);
        if (!file.exists())
            return null;
        /* 取得扩展名 */
        String end = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase(Locale.getDefault());

        return end;
    }

    public interface OnDownListener {
        void onResult(String path, boolean isSuccess);
    }

    public static void downLoadFile(String url, final OnDownListener mOnDownListener) {

        if (TextUtil.isEmpty(url))
            return;

        AppUpdateManager appUpdateManager = new AppUpdateManager(activity);

        appUpdateManager.download(url, new UpdateManager.IDownloadStatus() {
            @Override
            public void downloadFinish(String fileName) {
                if (mOnDownListener != null)
                    mOnDownListener.onResult(fileName, true);
            }

            @Override
            public void downloadPause() {
            }

            @Override
            public void downloadStart(long fileLength) {
            }

            @Override
            public void downLoading(int progress, int second) {
            }

            @Override
            public void downLoadError(String message) {

                if (mOnDownListener != null)
                    mOnDownListener.onResult(null, true);
            }
        });
    }

    //获取文件路径
    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null)
            data = uri.getPath();
        else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

    /**
     * 获取指定文件大小(单位：字节)
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static long getFileSize(File file) {
        if (file == null) {
            return 0;
        }
        long size = 0;
        try {
            if (file.exists()) {
                FileInputStream fis = null;
                fis = new FileInputStream(file);
                size = fis.available();
            }
        } catch (Exception e) {

        }
        return size;
    }
}
